package com.budwk.app.sqfw.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 气源使用情况
 */
@Getter
public enum AirSourceUsageEnum {

    ALL("全部", "全部"),
    NO("③", "无"),
    PETROLEUM_GAS("①", "液化石油气"),
    GAS("②", "城市燃气用二甲醚");

    private String code;

    private String name;

    AirSourceUsageEnum(String code, String name) {
        this.code = code;
        this.name = name;
    }
    /**
     * @return
     */
    public static String getCode(String name) {
        if (StringUtils.isEmpty(name)) {
            return StringUtils.EMPTY;
        }
        Optional<AirSourceUsageEnum> optional = Arrays.stream(AirSourceUsageEnum.values()).filter(e -> name.equals(e.getName())).findFirst();
        if (!optional.isPresent()) {
            return StringUtils.EMPTY;
        }
        return optional.get().getCode();
    }

    public static String getName(String code) {
        if (StringUtils.isEmpty(code)) {
            return StringUtils.EMPTY;
        }
        Optional<AirSourceUsageEnum> optional = Arrays.stream(AirSourceUsageEnum.values()).filter(e -> code.equals(e.getCode())).findFirst();
        if (!optional.isPresent()) {
            return StringUtils.EMPTY;
        }
        return optional.get().getName();
    }

    /**
     * 将编码转为中文
     * @param codeStr
     * @return
     */
    public static String handleCodesToName(String codeStr) {
        if (StringUtils.isEmpty(codeStr)) {
            return StringUtils.EMPTY;
        }
        List<String> names = new ArrayList<>();
        String[] codes = codeStr.split(",");
        for (String code : codes) {
            String name1 = getName(code);
            if (StringUtils.isNotEmpty(name1)) {
                names.add(name1);
            }
        }
        String newStr = names.stream().collect(Collectors.joining(","));
        return newStr;
    }

    public static List<String> listNames() {
        return Arrays.stream(AirSourceUsageEnum.values()).map(AirSourceUsageEnum::getName).collect(Collectors.toList());
    }
}
